package org.visitor.visitorserver.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.visitor.visitorserver.dto.*;
import org.visitor.visitorserver.entity.Professor;
import org.visitor.visitorserver.mapper.ProfessorMapper;
import org.visitor.visitorserver.service.ProfessorServiceInter;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/professor")
public class ProfessorController {
    @Autowired
    private ProfessorMapper professorMapper;
    @Autowired
    private ProfessorServiceInter professorServiceInter;
    @Autowired
    private FormatResponse formatResponse;

    @GetMapping("/selectAll")
    public FormatResponse selectAdmin() {
        try {
            List<Professor> li = professorMapper.selectList(null);
            formatResponse.setData(li);
            formatResponse.setSuccess(true);
            formatResponse.setCode(200);
        } catch (Exception e) {
            formatResponse.setSuccess(false);
            formatResponse.setCode(400);
            formatResponse.setMsg(e.getMessage());
        }
        return formatResponse;
    }

    @PostMapping("/selectById")
    public FormatResponse getOneAdmin(@RequestBody Map<String, String> requestBody) {
        try {
            String id = requestBody.get("id");
            Professor professor = professorMapper.selectById(id);
            formatResponse.setData(professor);
            formatResponse.setSuccess(true);
            formatResponse.setCode(200);
        } catch (Exception e) {
            formatResponse.setSuccess(false);
            formatResponse.setCode(400);
            formatResponse.setMsg(e.getMessage());
        }
        return formatResponse;
    }

    @PostMapping("/getProfessorList") // 通过姓名分页获取专家列表(PC)
    public FormatResponse getProfessorList(@RequestBody Map<String, String> requestBody) {
        try {
            ListWithTotal data = professorServiceInter.getProfessorList(requestBody);
            formatResponse.setData(data);
            formatResponse.setSuccess(true);
            formatResponse.setCode(200);
        } catch (Exception e) {
            formatResponse.setSuccess(false);
            formatResponse.setCode(400);
            formatResponse.setMsg(e.getMessage());
        }
        return formatResponse;
    }

    @PostMapping("/addProfessor") // 新增专家(PC)
    public FormatResponse addProfessor(@RequestBody Map<String, String> requestBody) {
        try {
            int data = professorServiceInter.insertOneProfessor(requestBody);
            formatResponse.setSuccess(true);
            formatResponse.setCode(200);
            formatResponse.setMsg("ok");
            formatResponse.setData(data);
        } catch (Exception e) {
            formatResponse.setSuccess(false);
            formatResponse.setCode(400);
            formatResponse.setMsg(e.getMessage());
        }
        return formatResponse;
    }

    @DeleteMapping("/deleteProfessor") // 删除专家(PC)
    public FormatResponse deleteProfessor(@RequestBody Map<String, String> requestBody) {
        try {
            int data = professorServiceInter.deleteOneProfessor(requestBody);
            formatResponse.setSuccess(true);
            formatResponse.setCode(200);
            formatResponse.setMsg("ok");
            formatResponse.setData(data);
        } catch (Exception e) {
            formatResponse.setSuccess(false);
            formatResponse.setCode(400);
            formatResponse.setMsg(e.getMessage());
        }
        return formatResponse;
    }

    @PostMapping("/editProfessor") // 修改专家(PC)
    public FormatResponse editProfessor(@RequestBody Map<String, String> requestBody) {
        try {
            int data = professorServiceInter.updateOneProfessor(requestBody);
            formatResponse.setSuccess(true);
            formatResponse.setCode(200);
            formatResponse.setMsg("ok");
            formatResponse.setData(data);
        } catch (Exception e) {
            formatResponse.setSuccess(false);
            formatResponse.setCode(400);
            formatResponse.setMsg(e.getMessage());
        }
        return formatResponse;
    }

    @PostMapping("/getProfessorNameList") // 通过名字获取专家列表(手机端)
    public FormatResponse getProfessorNameList(@RequestBody Map<String, String> requestBody) {
        try {
            List<ProfessorDtoType1> list = professorServiceInter.getProfessorNameList(requestBody);
            formatResponse.setData(list);
            formatResponse.setSuccess(true);
            formatResponse.setCode(200);
        } catch (Exception e) {
            formatResponse.setSuccess(false);
            formatResponse.setCode(400);
            formatResponse.setMsg(e.getMessage());
        }
        return formatResponse;
    }

    @PostMapping("/getProfessorTop3List") // 获取top3专家列表
    public FormatResponse getProfessorTop3List(@RequestBody Map<String, String> requestBody) {
        try {
            List<ProfessorDtoType2> list = professorServiceInter.getProfessorTop3List(requestBody);
            formatResponse.setData(list);
            formatResponse.setSuccess(true);
            formatResponse.setCode(200);
        } catch (Exception e) {
            formatResponse.setSuccess(false);
            formatResponse.setCode(400);
            formatResponse.setMsg(e.getMessage());
        }
        return formatResponse;
    }

    @PostMapping("/getProfessorTop10List") // 获取top3专家列表
    public FormatResponse getProfessorTop10List(@RequestBody Map<String, String> requestBody) {
        try {
            List<ProfessorDtoType3> list = professorServiceInter.getProfessorTop10List(requestBody);
            formatResponse.setData(list);
            formatResponse.setSuccess(true);
            formatResponse.setCode(200);
        } catch (Exception e) {
            formatResponse.setSuccess(false);
            formatResponse.setCode(400);
            formatResponse.setMsg(e.getMessage());
        }
        return formatResponse;
    }

    @PostMapping("/roleConfirm") // 专家审核权限确认
    public FormatResponse roleConfirm(@RequestBody Map<String, String> requestBody) {
        try {
            Professor data = professorServiceInter.roleConfirm(requestBody);
            formatResponse.setData(data);
            formatResponse.setSuccess(true);
            formatResponse.setCode(200);
        } catch (Exception e) {
            formatResponse.setSuccess(false);
            formatResponse.setCode(400);
            formatResponse.setMsg(e.getMessage());
        }
        return formatResponse;
    }
}
